/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2020-12-12 17:40:20
 * @LastEditors: ding-cx
 * @LastEditTime: 2020-12-12 18:04:01
 * @Description: egg扩展，扩展context对象
 */
const config = require("../../config/ding.config.js").status_code;

module.exports = {
    /* 封装send方法，ctx.send直接输出数据。 */
    send(data, status = 20000, msg = "") {
        const sendData = config[status] || config[50000];
        sendData.data = data;
        msg && (sendData.msg = msg);
        //  this 就是ctx对象
        this.body = sendData;
    },
    /* 封装echo方法，ctx.echo 是往body上添加一个key value。可多次调用而不会覆盖。send方法是直接覆盖整个body */
    echo(obj = {}, status = 20000, msg = "") {
        //  console.log(config.status_code);
        if (Object.prototype.toString.call(obj) === '[object Object]') {
            if (Object.prototype.toString.call(this.body) === '[object Object]') {
                Object.keys(obj).forEach(item => {
                    this.body['data'][item] = obj[item]
                })
            } else {
                const sendData = config[status] || config[50000]
                sendData.data = obj;
                msg && (sendData.msg = msg);
                this.body = sendData;
            }
        }
    }
}
